﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Csharphomework4_1
{
    class OrderService
    {
        List<Order> orderlist=new List<Order>();

        public OrderService()
        {
            orderlist.Add(new Order(201606, "overwatch","dva", 329));
            orderlist.Add(new Order(202009, "apex", "valkiri", 0));
            orderlist.Add(new Order(201020, "deadbydaylight", "leon", 129));


        }
        public void Export()
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream("s.xml", FileMode.Create))
            {
                xmlSerializer.Serialize(fs, orderlist);
            }
        }

        public void Import()
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream("s.xml", FileMode.Open))
            {
                List<Order> orders = (List<Order>)xmlSerializer.Deserialize(fs);
            }
        }
        public void addOrder(long orderNumber, string objectName, string customer, int cost)
        {
            Order order=new Order(orderNumber, objectName, customer, cost);
            orderlist.Add(order);
            show();
        }

        public void deleteOrder(long orderNumber, string objectName, string customer, int cost)
        {
            Order order = new Order(orderNumber, objectName, customer, cost);
            try
            {
                foreach (Order simpleorder in orderlist)
                {
                    if (simpleorder == order)
                    {
                        orderlist.Remove(simpleorder);

                    }
                }
            }
            catch (Exception)
            {
                Console.WriteLine("删除错误");
            }
        }

        public List<Order> findOrder(int i)
        {
            try
            {
                List<Order> orders = null;




                var content = Console.ReadLine();
                switch (i)
                {
                    case 1:
                        orders = (List<Order>)(from s in orderlist
                                               where s.orderNumber.ToString() == content
                                               orderby s.cost
                                               select s);

                        break;
                    case 2:
                        orders = (List<Order>)(from s in orderlist
                                               where s.objectName == content
                                               orderby s.cost
                                               select s);

                        break;
                    case 3:
                        orders = (List<Order>)(from s in orderlist
                                               where s.customer == content
                                               orderby s.cost
                                               select s);

                        break;
                    case 4:
                        orders = (List<Order>)(from s in orderlist
                                               where s.cost.ToString() == content
                                               orderby s.cost
                                               select s);

                        break;
                }
                show();
                return orders;
            }
            catch (Exception)
            {
                show();
                Console.WriteLine("查询错误");
                return null;
            }

        }
        
        public void show()
        {
            foreach(Order order in orderlist)
            {
                Console.WriteLine(order);
            }
        }
    }
}
